package vn.com.bookstore.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.com.bookstore.domain.model.PagingInfo;
import vn.com.bookstore.domain.model.SearchResult;
import vn.com.bookstore.model.vo.CustomerVO;
import vn.com.bookstore.model.vo.TransactionVO;
import vn.com.bookstore.service.CustomerService;
import vn.com.bookstore.service.TransactionService;

@Controller
public class TransactionController {
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private TransactionService transactionService;
	
	@RequestMapping(value = "/online/view-transaction", method = RequestMethod.GET)
	public String viewTransaction(Model model, @RequestParam("pageIndex") int pageIndex) {
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		CustomerVO customerVO = customerService.getCustomerVOByUsername(username);
		PagingInfo pagingInfo = new PagingInfo(10, pageIndex);
		SearchResult<TransactionVO> result = transactionService.getTransactionByCustomerId(pagingInfo, customerVO.getId());
		List<TransactionVO> transactions = result.getSearchResult();
		PagingInfo pagingInfoResult = result.getPagingInfo();
		model.addAttribute("transactions", transactions);
		model.addAttribute("pagingInfo", pagingInfoResult);
		return "website/transactions/view-transaction";
	}
}
